package gendoc

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

// GenerateDocHandler 处理AI文档生成请求 (JSON格式)
func GenerateDocHandler(c *gin.Context) {
	var req GenerateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"message": "参数错误: " + err.Error(),
		})
		return
	}

	service := NewAIDocService()
	response, err := service.GenerateDoc(&req)
	if err != nil {
		logrus.Errorf("AI文档生成失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"success": false,
			"message": "AI文档生成失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    response,
	})
}

// GenerateDocFormHandler 处理AI文档生成请求 (表单格式)
func GenerateDocFormHandler(c *gin.Context) {
	var req GenerateRequest
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"success": false,
			"message": "参数错误: " + err.Error(),
		})
		return
	}

	service := NewAIDocService()
	response, err := service.GenerateDoc(&req)
	if err != nil {
		logrus.Errorf("AI文档生成失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"success": false,
			"message": "AI文档生成失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    response,
	})
}